package com.ruoyi.web.controller.system;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ActivityManagement;
import com.ruoyi.system.service.IActivityManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * 活动日历Controller
 *
 */
@RestController
@RequestMapping("/system/activity/calendar")
public class ActivityCalendarController extends BaseController {


    @Autowired
    private IActivityManagementService activityManagementService;

    /**
     * 查询当月所有活动列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody ActivityManagement activityManagement)
    {
        String queryYear = activityManagement.getQueryYear();
        String queryMonth = activityManagement.getQueryMonth();
        if(StringUtils.isBlank(queryYear)){
            return AjaxResult.error("查询年份不能为空");
        }
        if(StringUtils.isBlank(queryMonth)){
            return AjaxResult.error("查询月份不能为空");
        }
        String firstDayOfMonth = DateUtils.getFirstDayOfMonth(Integer.valueOf(queryYear), Integer.valueOf(queryMonth));
        String lastDayOfMonth = DateUtils.getLastDayOfMonth(Integer.valueOf(queryYear), Integer.valueOf(queryMonth));
        activityManagement.setActivityStartTime(firstDayOfMonth);
        activityManagement.setActivityEndTime(lastDayOfMonth);
        List<ActivityManagement> list = activityManagementService.selectActivityManagementList(activityManagement);
        return AjaxResult.success(list);
    }

    /**
     * 查询当月活动详情
     */
    @PreAuthorize("@ss.hasPermi('system:management:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(activityManagementService.selectActivityManagementById(id));
    }


    /**
     * 查询当月我参加的活动列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @PostMapping("/joinlist")
    public AjaxResult joinlist(@RequestBody ActivityManagement activityManagement)
    {
        String queryYear = activityManagement.getQueryYear();
        String queryMonth = activityManagement.getQueryMonth();
        if(StringUtils.isBlank(queryYear)){
            return AjaxResult.error("查询年份不能为空");
        }
        if(StringUtils.isBlank(queryMonth)){
            return AjaxResult.error("查询月份不能为空");
        }
        String firstDayOfMonth = DateUtils.getFirstDayOfMonth(Integer.valueOf(queryYear), Integer.valueOf(queryMonth));
        String lastDayOfMonth = DateUtils.getLastDayOfMonth(Integer.valueOf(queryYear), Integer.valueOf(queryMonth));
        activityManagement.setActivityStartTime(firstDayOfMonth);
        activityManagement.setActivityEndTime(lastDayOfMonth);
        List<ActivityManagement> list = activityManagementService.selectJoinActivityManagementList(activityManagement);
        return AjaxResult.success(list);
    }

    /**
     * 查询当月我已参的活动列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @PostMapping("/joinedlist")
    public AjaxResult joinedlist(@RequestBody ActivityManagement activityManagement)
    {
        String queryYear = activityManagement.getQueryYear();
        String queryMonth = activityManagement.getQueryMonth();
        if(StringUtils.isBlank(queryYear)){
            return AjaxResult.error("查询年份不能为空");
        }
        if(StringUtils.isBlank(queryMonth)){
            return AjaxResult.error("查询月份不能为空");
        }
        String firstDayOfMonth = DateUtils.getFirstDayOfMonth(Integer.valueOf(queryYear), Integer.valueOf(queryMonth));
        String lastDayOfMonth = DateUtils.getLastDayOfMonth(Integer.valueOf(queryYear), Integer.valueOf(queryMonth));
        activityManagement.setActivityStartTime(firstDayOfMonth);
        activityManagement.setActivityEndTime(lastDayOfMonth);
        List<ActivityManagement> list = activityManagementService.selectjoinedlist(activityManagement);
        return AjaxResult.success(list);
    }

    /**
     * 查询当月我发起的活动列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @PostMapping("/createlist")
    public AjaxResult createlist(@RequestBody ActivityManagement activityManagement)
    {
        String queryYear = activityManagement.getQueryYear();
        String queryMonth = activityManagement.getQueryMonth();
        if(StringUtils.isBlank(queryYear)){
            return AjaxResult.error("查询年份不能为空");
        }
        if(StringUtils.isBlank(queryMonth)){
            return AjaxResult.error("查询月份不能为空");
        }
        String firstDayOfMonth = DateUtils.getFirstDayOfMonth(Integer.valueOf(queryYear), Integer.valueOf(queryMonth));
        String lastDayOfMonth = DateUtils.getLastDayOfMonth(Integer.valueOf(queryYear), Integer.valueOf(queryMonth));
        activityManagement.setActivityStartTime(firstDayOfMonth);
        activityManagement.setActivityEndTime(lastDayOfMonth);
        List<ActivityManagement> list = activityManagementService.selectActivityManagementList(activityManagement);
        return AjaxResult.success(list);
    }
}
